package com.itheima.tlias84.controller;

import com.itheima.tlias84.dto.SelectStudentByIdDTO;
import com.itheima.tlias84.po.InsterStudentParam;
import com.itheima.tlias84.po.SelectStudentPageParam;
import com.itheima.tlias84.po.UpdateStudentParam;
import com.itheima.tlias84.service.StudentService;
import com.itheima.tlias84.utils.ResultUtil;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/students")
public class StudentController {
    @Resource
    StudentService studentService;
    @GetMapping
    public ResultUtil selectStudentByPage(SelectStudentPageParam param) {
        if (Objects.isNull(param.getPage())|| Objects.isNull(param.getPageSize())){
            return ResultUtil.fail("分页参数不能为空");
        }
        return studentService.selectStudentByPage(param);
    }
    @PostMapping
    private ResultUtil insertStudent(@RequestBody InsterStudentParam param) {
        int i = studentService.insertStudent(param);
        return i ==0 ? ResultUtil.fail("添加失败") : ResultUtil.success("添加成功");
    }
    @DeleteMapping
    public ResultUtil batchDelete(@RequestParam("ids")List<Integer> ids){
        if (ids.isEmpty()){
            return ResultUtil.fail("请先选择要删除的id");
        }
        int i = studentService.batchDelete(ids);
        return ResultUtil.success(i);
    }
    @GetMapping("/{id}")
    public ResultUtil selectStudentById(@PathVariable("id") Integer id){
        SelectStudentByIdDTO dto = studentService.selectStudentById(id);
        return ResultUtil.success(dto);
    }
    @PutMapping
    public ResultUtil updateStudent(@RequestBody UpdateStudentParam param){
        int i = studentService.updateStudent(param);
        return i == 0 ? ResultUtil.fail("修改失败") : ResultUtil.success("修改成功");
    }
    @PutMapping("/violation/{id}/{score}")
    public ResultUtil violation(@PathVariable("id") Integer id){
        int i = studentService.violation(id);
        return i == 0 ? ResultUtil.fail("修改失败") : ResultUtil.success("修改成功");
    }
}
